package com.gewu.bookeeping.View.fragment;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.gewu.bookeeping.MyDb.bean.ALIBeanIn;
import com.gewu.bookeeping.MyDb.bean.WeChtBeanIn;
import com.gewu.bookeeping.MyDb.dao.ALiDaoInPerson;
import com.gewu.bookeeping.MyDb.dao.WeChtDaoInPerson;
import com.gewu.bookeeping.R;
import com.gewu.bookeeping.Utils.AppCot;
import com.gewu.bookeeping.View.ItemFragment.oneItem1Fragment;
import com.gewu.bookeeping.View.ItemFragment.oneItem2Fragment;
import com.gewu.bookeeping.View.ItemFragment.oneItem3Fragment;
import com.gewu.bookeeping.View.adapter.onePagerAdapter;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.textfield.TextInputLayout;

import java.util.ArrayList;
import java.util.List;

public class oneFragment extends Fragment implements Toolbar.OnMenuItemClickListener {

    private com.gewu.bookeeping.View.ItemFragment.oneItem1Fragment oneItem1Fragment = new oneItem1Fragment();
    private com.gewu.bookeeping.View.ItemFragment.oneItem2Fragment oneItem2Fragment = new oneItem2Fragment();
    private com.gewu.bookeeping.View.ItemFragment.oneItem3Fragment oneItem3Fragment = new oneItem3Fragment();
    private TabLayout oneGroup;
    private ViewPager oneContent;
    private onePagerAdapter onePagerAdapter;
    private List<String> tabs = new ArrayList<>();

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View inflate = LayoutInflater.from(container.getContext()).inflate(R.layout.activity_one_fragment, container, false);
        initView(inflate);
        initList();
        return inflate;
    }

    /**
     * 初始化tablayout
     */
    private void initList() {
        if (AppCot.getwechart(getContext())) {
            onePagerAdapter.addFragemnt(oneItem1Fragment);
            onePagerAdapter.addTab("微信");
        } else {
            onePagerAdapter.removdFra(oneItem1Fragment, "微信");
        }

        if (AppCot.getali(getContext())) {
            onePagerAdapter.addFragemnt(oneItem2Fragment);
            onePagerAdapter.addTab("支付宝");
        } else {
            onePagerAdapter.removdFra(oneItem2Fragment, "支付宝");
        }

        if (AppCot.getmes(getContext())) {
            onePagerAdapter.addFragemnt(oneItem3Fragment);
            onePagerAdapter.addTab("短信");
        } else {
            onePagerAdapter.removdFra(oneItem3Fragment, "短信");
        }

        if (!AppCot.getmes(getContext()) && !AppCot.getwechart(getContext()) && !AppCot.getali(getContext())) {
            onePagerAdapter.addFragemnt(oneItem3Fragment);
            onePagerAdapter.addTab("短信");
        }

        oneGroup.setupWithViewPager(oneContent);
        oneContent.setAdapter(onePagerAdapter);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        onePagerAdapter.notifyDataSetChanged();
    }

    /**
     * 初始化布局
     *
     * @param inflater
     */
    private void initView(View inflater) {
        oneContent = inflater.findViewById(R.id.viewpager);
        oneGroup = inflater.findViewById(R.id.one_tab);
        Toolbar toolbar = (Toolbar) inflater.findViewById(R.id.toolbar);
        toolbar.setTitle("收入和支出");
        toolbar.setLogo(R.mipmap.one_ic);
        toolbar.setOnMenuItemClickListener(this);
        toolbar.inflateMenu(R.menu.toolbarmenu);
        onePagerAdapter = new onePagerAdapter(getChildFragmentManager());
    }

    /**
     * 当fragmeny显示的时候会进行再次操作
     *
     * @param hidden
     */
    @Override
    public void onHiddenChanged(boolean hidden) {
        if (!hidden) {
            initList();
        }
    }


    @Override
    public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
        inflater.inflate(R.menu.toolbarmenu,menu);
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {

        return super.onOptionsItemSelected(item);
    }

    private void showAddWindow() {
        final AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
        View view2 = View.inflate(getContext(), R.layout.add_layout, null);
        final TextInputLayout viewById1 = view2.findViewById(R.id.name);
        final EditText editText = view2.findViewById(R.id.money);
        final CheckBox c = view2.findViewById(R.id.w);
        final CheckBox c1 = view2.findViewById(R.id.a);
        Button login = view2.findViewById(R.id.login);
        builder.setTitle("添加").setView(view2);
        builder.create().show();
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (viewById1.getEditText().getText().toString()==null||editText.getText().toString()==null){
                    Toast.makeText(getContext(), "内容不能为NULL", Toast.LENGTH_SHORT).show();
                }else {
                    if (!c.isChecked()){
                        Toast.makeText(getContext(), "勾选方式", Toast.LENGTH_SHORT).show();
                    }else {
                        if (c.isChecked()){
                            WeChtDaoInPerson weChtDaoInPerson = new WeChtDaoInPerson(getContext());
                            WeChtBeanIn weChtBeanIn = new WeChtBeanIn();
                            weChtBeanIn.setType("添加");
                            weChtBeanIn.setTime(AppCot.getTime());
                            weChtBeanIn.setMoney(Float.valueOf(editText.getText().toString()));
                            weChtBeanIn.setText(viewById1.getEditText().getText().toString());
                            weChtDaoInPerson.add(weChtBeanIn);
                        }else if (c1.isChecked()){
                            ALiDaoInPerson daoInPerson = new ALiDaoInPerson(getContext());
                            ALIBeanIn aliBeanIn = new ALIBeanIn();
                            aliBeanIn.setType("添加");
                            aliBeanIn.setTime(AppCot.getTime());
                            aliBeanIn.setMoney(Float.valueOf(editText.getText().toString()));
                            aliBeanIn.setText(viewById1.getEditText().getText().toString());
                            daoInPerson.add(aliBeanIn);
                        }
                        viewById1.getEditText().setText("");
                        editText.setText("");
                    }
                }
            }
        });
    }

    @Override
    public boolean onMenuItemClick(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.id_menu_report:
                Log.i("TAG", "onOptionsItemSelected: ");
                showAddWindow();
                break;
        }
        return true;
    }
}